package com.sophon.pattern.singleton;

/**
 * 懒汉模式，在使用时才生成实例。
 * 
 * @author Andy
 *
 */
public class LazySingletonInnerClass {

	static {
		System.out.println("outer class initialized");
	}

	private LazySingletonInnerClass() {
		synchronized (LazySingletonInnerClass.class) {
			if (Innerclass.INSTANCE != null) {
				throw new RuntimeException("A instance has been created");
			}
		}
	}

	/**
	 * Inner class在外部类方法被调用时加载。
	 * 
	 * @return
	 */
	public static final LazySingletonInnerClass getInstance() {
		return Innerclass.INSTANCE;
	}

	private static class Innerclass {
		private static final LazySingletonInnerClass INSTANCE = new LazySingletonInnerClass();
		static {
			System.out.println("inner class initialized");
		}
	}
}
